package com.jmp.api.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.service.ImService;
import com.jmp.base.ImApi;
import com.jmp.base.dto.ImChatPageRequest;
import com.jmp.base.dto.ImChatPageResponse;
import com.jmp.base.dto.ImRequest;
import com.jmp.base.dto.ImSendRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.model.Chat;
import one.stand.model.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("聊天聊天控制器")
@RestController
@Slf4j
public class ImController implements ImApi {

    @Autowired
    private ImService imService;

    @Override
    @ApiOperation(value = "发送")
    public ResultModel imSend(@RequestBody ImSendRequest request) {
        return ResultModel.success(imService.send(request));
    }

    @Override
    @ApiOperation(value = "聊天列表")
    public ResultModel imChat(@RequestBody ImRequest request) {
        return ResultModel.success(imService.getImChat(request.getId()));
    }

    @Override
    public ResultModel<ImChatPageResponse> chatPage(@RequestBody ImChatPageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Chat> chats = imService.getImChat(request.getId());
        PageResponse2<ImChatPageResponse.Chat> chatPageResponse2 = ConvertUtils.page2(chats, chat -> ImChatPageResponse.Chat.builder()
                .id(chat.getId())
                .cid(chat.getCid())
                .userid(chat.getUserid())
                .targetUserId(chat.getTargetUserId())
                .targetAvatar(chat.getTargetAvatar())
                .targetUsername(chat.getTargetUsername())
                .lastcontent(chat.getLastcontent())
                .lasttime(chat.getLasttime())
                .ctime(chat.getCtime())
                .online(chat.getOnline())
                .appOnline(chat.getAppOnline())
                .build());
        ImChatPageResponse response = ImChatPageResponse.builder().chats(chatPageResponse2).build();
        return ResultModel.success(response);
    }

    @Override
    @ApiOperation(value = "聊天内容")
    public ResultModel imChatContent(@RequestBody ImRequest request) {
        return ResultModel.success(imService.getChatContent(request));
    }
}
